package com.zd.service.impl;

import com.github.pagehelper.PageHelper;
import com.zd.mapper.InfoManageMapper;
import com.zd.model.InfoManage;
import com.zd.service.IInfoManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class InfoManageServiceImpl implements IInfoManageService {

    @Autowired
    private InfoManageMapper infoManageMapper;


    @Override
    public List<InfoManage> selectInfoManage(int pageNum, int pageSize, Map<String, Object> queryMap) {
        PageHelper.startPage(pageNum, pageSize);

        List<InfoManage> infoManageList = infoManageMapper.selectInfoManage(queryMap);

        return infoManageList;
    }

    @Override
    public List<InfoManage> selectInfo(int pageNum,int pageSize,Map<String, Object> queryMap) {
        PageHelper.startPage(pageNum,pageSize);
        queryMap.put("type",1);
        List<InfoManage> infoManageList = infoManageMapper.selectInfoManage(queryMap);

        return infoManageList;
    }
    @Override
    public List<InfoManage> selectPolicyDocumentManage(int pageNum,int pageSize,Map<String, Object> queryMap) {
        PageHelper.startPage(pageNum,pageSize);
        queryMap.put("type",2);
        List<InfoManage> policyDocumentList = infoManageMapper.selectInfoManage(queryMap);

        return policyDocumentList;
    }
    @Override
    public List<InfoManage> selectSchoolManage(int pageNum,int pageSize,Map<String, Object> queryMap) {
        PageHelper.startPage(pageNum,pageSize);
        queryMap.put("type",3);
        List<InfoManage> schoolList = infoManageMapper.selectInfoManage(queryMap);

        return schoolList;
    }

    @Override
    public boolean insertSelective(InfoManage infoManage) {

        infoManageMapper.insertSelective(infoManage);
        return true;
    }

    @Override
    public InfoManage selectInfoManageById(int id) {

        Map<String,Object> queryMap=new HashMap<String,Object>();

        queryMap.put("id",id);

        InfoManage infoManage = infoManageMapper.selectInfoManage(queryMap).get(0);

        //拆分协议地址
        if(infoManage.getFileurl()!=null && !infoManage.getFileurl().equals("")){

            List<String> list=new ArrayList<String>();

            String[] split = infoManage.getFileurl().split(",");

            for (String str:split) {

                list.add(str);

            }

            infoManage.setFileurlList(list);
        }

        return infoManage;
    }

    @Override
    public boolean updateSelective(InfoManage infoManage) {
        //判断合作协议是否有值，如果有值表明有新上传的合作协议，累加修改数据库
        String fileurl = infoManage.getFileurl();

        if(fileurl!=null && !fileurl.equals("")){
            //获取数据库原先合作协议值进行累加
            Map<String,Object> queryMap=new HashMap<String,Object>();
            queryMap.put("id",infoManage.getId());

            InfoManage workInfoOld = infoManageMapper.selectInfoManage(queryMap).get(0);

            infoManage.setFileurl(workInfoOld.getFileurl()+fileurl);
        }
        infoManageMapper.updateByPrimaryKeySelective(infoManage);
        return true;
    }

    @Override
    public boolean deleteFileurlName(int id, String deleteFileurlName) {

        Map<String,Object> queryMap=new HashMap<String,Object>();
        queryMap.put("id",id);

        InfoManage info = infoManageMapper.selectInfoManage(queryMap).get(0);

        //获取数据库合作协议字段
        String fileurls = info.getFileurl();

        //替换掉原先合作协议字符串
        String fileurlReplace = fileurls.replace(deleteFileurlName + ",", "");

        //修改数据库
        InfoManage updateInfoManage=new InfoManage();

        updateInfoManage.setId(new Long(id));

        updateInfoManage.setFileurl(fileurlReplace);

        infoManageMapper.updateByPrimaryKeySelective(updateInfoManage);

        return true;
    }

    @Override
    public boolean deleteInfoManage(int id) {

        InfoManage infoManage=new InfoManage();

        infoManage.setId(new Long(id));
        //逻辑删除  flag为0
        infoManage.setFlag(0);

        infoManageMapper.updateByPrimaryKeySelective(infoManage);


        return true;
    }
}
